package com.skiving.cloud.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.skiving.cloud.admin.entity.SysMenu;
import com.skiving.cloud.admin.entity.SysOrg;
import com.skiving.cloud.admin.entity.SysTenantMenu;
import com.skiving.cloud.admin.mapper.SysMenuMapper;
import com.skiving.cloud.admin.mapper.SysOrgMapper;
import com.skiving.cloud.admin.mapper.SysTenantMenuMapper;
import com.skiving.cloud.auth.constant.AuthConstant;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.admin.entity.SysTenant;
import com.skiving.cloud.admin.mapper.SysTenantMapper;
import com.skiving.cloud.admin.service.SysTenantService;
import org.noear.solon.annotation.Component;
import org.noear.solon.Utils;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Transaction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 租户表 服务层实现。
 *
 * @author skivingcloud
 * @since 2025-03-08
 */
@Component
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant>  implements SysTenantService {
    @Inject
    private SysMenuMapper sysMenuMapper;
    @Inject
    private SysTenantMenuMapper sysTenantMenuMapper;
    @Inject
    private SysOrgMapper sysOrgMapper;
    /**
     * 分页查询租户表
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysTenant> queryByPage(PageParameter<SysTenant> parameter){
        Page<SysTenant> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (Utils.isNotBlank(parameter.getParam().getId())) {
                queryWrapper.and(SysTenant::getId).like(parameter.getParam().getId());
            }
            if (Utils.isNotBlank(parameter.getParam().getTenantName())) {
                queryWrapper.and(SysTenant::getTenantName).like(parameter.getParam().getTenantName());
            }
            if (Utils.isNotBlank(parameter.getParam().getTenantCode())) {
                queryWrapper.and(SysTenant::getTenantCode).like(parameter.getParam().getTenantCode());
            }
            if (Utils.isNotBlank(parameter.getParam().getCreater())) {
                queryWrapper.and(SysTenant::getCreater).like(parameter.getParam().getCreater());
            }
            if (Utils.isNotBlank(parameter.getParam().getModUser())) {
                queryWrapper.and(SysTenant::getModUser).like(parameter.getParam().getModUser());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 查询租户有权限的资源树
     *
     * @param tenantId 租户ID
     * @return 树形菜单
     */
    @Override
    public List<SysMenu> getTenantMenuList(String tenantId) {
        QueryWrapper queryWrapper = QueryWrapper.create().from(SysMenu.class).as("m").join(SysTenantMenu.class).as("tm").on(
                wrapper -> wrapper.where(SysMenu::getId).eq(SysTenantMenu::getMenuId)
        ).where(SysTenantMenu::getTenantId).eq(tenantId);
        List<SysMenu> listAll = sysMenuMapper.selectListByQuery(queryWrapper);
        return listAll.stream().filter(item -> {
            item.setChildren(listAll.stream().filter(e -> e.getParentId().equals(item.getId())).toList());
            return item.getParentId().equals("0");
        }).toList();
    }

    /**
     * 查询租户信息{key:'', value:''}
     *
     * @return 租户信息{key:'', value:''}
     */
    @Override
    public List<Map<String, Object>> getTenantSelect() {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(SysTenant.class).orderBy(SysTenant::getCreateTime, false);
        return this.mapper.selectListByQuery(queryWrapper).stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("key", m.getId());
            map.put("value", m.getTenantName());
            return map;
        }).toList();
    }

    /**
     * 批量删除租户信息
     *
     * @param ids 租户id数字
     */
    @Override
    @Transaction
    public void deleteBatch(List<String> ids) {
        //删除租户资源关系
        QueryWrapper tm = QueryWrapper.create().where(SysTenantMenu::getTenantId).in(ids);
        sysTenantMenuMapper.deleteByQuery(tm);
        //删除租户机构(逻辑删除)
        QueryWrapper tmOrg = QueryWrapper.create().where(SysOrg::getTenantId).in(ids);
        SysOrg org = new SysOrg();
        org.setOrgStatus(AuthConstant.COMMON_STATUS_DELETED);
        sysOrgMapper.updateByQuery(org, true, tmOrg);
        this.mapper.deleteBatchByIds(ids);
    }
}
